﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TinkleDao.Interfaces;
using System.Linq.Expressions;

namespace TinkleDao
{
    public abstract class AbstractCrudDao<T> : IBaseCrudDao<T> where T : class
    {
        tinkleEntities1 context = new tinkleEntities1(ConexaoBD.GetConnectionStrings());

        public void Add(T pEntity)
        {
            context.AddObject(pEntity.GetType().Name, pEntity);
        }

        public void Delete(T pEntity)
        {

            context.DeleteObject(pEntity);

        }

        public void Attach(T pEntity)
        {

            context.AttachTo(pEntity.GetType().Name, pEntity);

        }

        public void Detach(T pEntity)
        {

            context.Detach(pEntity);

        }

        public void Update(T pEntity)
        {

            context.ApplyCurrentValues<T>(pEntity.GetType().Name, pEntity);

        }

        public IQueryable<T> Find(Expression<Func<T, bool>> where)
        {

            return context.CreateObjectSet<T>().Where(where);

        }

        public IQueryable<T> GetAll()
        {

            return context.CreateObjectSet<T>();

        }

        public void SaveChanges()
        {

            context.SaveChanges();

        }

        public tinkleEntities1 Contexto()
        {
            return context;
        }
    }
}
